//
//  StatusCellTopView.swift
//  HMWeibo
//
//  Created by wulei on 15/12/11.
//  Copyright © 2015年 wulei. All rights reserved.
//

import UIKit
import SnapKit


class StatusCellTopView: UIView {
    
    var ViewModel: HMStatusViewModel? {
        
        didSet{
            
// UIImage imageNamed 创建的图像,缓存由系统来管理,程序员不能直接释放
            // 适用于小的图片素材
            // 高清大图千万不要使用这个素材
            
            // 用户名称
            nameLabel.text = ViewModel?.status.user?.screen_name
            
            // 用户头像
            iconView.sd_setImageWithURL(ViewModel?.userProfileURL, placeholderImage: ViewModel?.userPlaceholderImage)
            
            // 用户会员级别
            memberIconView.image = ViewModel?.userMemberImage
            
            // 用户认证级别
            vipIconView.image = ViewModel?.userVipImage
            
            // 时间
            // TODO - 后续会讲
            timeLabel.text = "刚刚"//ViewModel?.status.created_at
            
            // 来源
            sourceLabel.text = "黑马"//ViewModel?.status.source
            
            
            
            
            
            
            
            
            
            
        }
    }
    
    
    
    
    
// MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 添加控件
    // 头像图标
    private lazy var iconView: UIImageView = UIImageView(imageName: "avatar_default_big")
    // 姓名标签
    private lazy var nameLabel: UILabel = UILabel(title: "王老五", fontSize: 14)
    // 认证图标
    private lazy var memberIconView: UIImageView = UIImageView(imageName: "common_icon_membership_level1")
    // 会员图标
    private lazy var vipIconView: UIImageView = UIImageView(imageName: "avatar_vip")
    // 时间标签
    
   private lazy var timeLabel: UILabel = UILabel(title: "现在", fontSize: 11, color: UIColor.orangeColor())
    // 来源标签
    private lazy var sourceLabel: UILabel = UILabel(title: "来源", fontSize: 11)
}

// MARK: -设置界面
extension StatusCellTopView {
    func setupUI() {
        backgroundColor = UIColor.whiteColor()
        // 添加控件
        let sepView = UIView()
        sepView.backgroundColor = UIColor.lightGrayColor()
        addSubview(sepView)
        
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(vipIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        
        
        // 设置约束
        // 分隔视图
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.height.equalTo(StatusCellMargin)
        }
        
        // 头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.size.equalTo(StatusCellIconWidth)
        }
        
        // 姓名
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        // 认证图标
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            
        }
        
        // 会员图标
        vipIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        // 时间标签
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        // 消息来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        
        
        
        
        
        
        
        
    }
}